package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

@Service
public class WorkspaceServicelmpl implements WorkspaceService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    /**
     * 今日运营数据查询
     * @param begintime
     * @param endtime
     * @return
     */
    @Override
    public BusinessDataVO lookbusinessData(LocalDateTime begintime, LocalDateTime endtime) {
        //用map封装所需要查询到的对象
        HashMap<Object, Object> map = new HashMap<>();
        map.put("begin",begintime);
        map.put("end",endtime);
        //每日订单总数
        Integer orderCount = orderMapper.getcountBy(map);
        //每日有效订单数
        map.put("status", Orders.COMPLETED);
        Integer vailOrderCount = orderMapper.getcountBy(map);
        //获取查询新增用户
        Integer newUsers = userMapper.getcount(map);
        //订单完成率
        Double orderCompletionRate=0.0;
        if (orderCount != 0) {
            orderCompletionRate = vailOrderCount.doubleValue() / orderCount;
        }
        //获取营业总额
        Double turnover = orderMapper.sum(map);
        //当营业额为null时，赋值0.0
        if (turnover == null) {
            turnover = 0.00;
        }
        //平均客单价
        Double unitPrice=0.0;
        if(vailOrderCount!=0){
            unitPrice=turnover/vailOrderCount;
        }
        BusinessDataVO businessDataVO = BusinessDataVO.builder().newUsers(newUsers).
                orderCompletionRate(orderCompletionRate).
                turnover(turnover).unitPrice(unitPrice).
                validOrderCount(vailOrderCount).build();
        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        //封装集合map
        HashMap<Object, Object> map = new HashMap<>();
        map.put("begin", LocalDateTime.of(LocalDate.now(), LocalTime.MIN));
        //全部订单
        map.put("status",null);
        Integer allOrders = orderMapper.getcountBy(map);
        //已取消数量
        map.put("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.getcountBy(map);
        //已完成数量
        map.put("status",Orders.COMPLETED);
        Integer completedOrders = orderMapper.getcountBy(map);
        // 待派送成数量
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.getcountBy(map);
        //待接单数量
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.getcountBy(map);
        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder().allOrders(allOrders).
                deliveredOrders(deliveredOrders).
                cancelledOrders(cancelledOrders).
                completedOrders(completedOrders).
                waitingOrders(waitingOrders).build();
        return orderOverViewVO;
    }

    @Override
    public DishOverViewVO overviewDishes() {
        //封装查询map
        HashMap<Object, Object> map = new HashMap<>();
        //已起售菜品数量
        map.put("status", StatusConstant.ENABLE);
       Integer sold=dishMapper.getcount(map);
       //已停售菜品数量
        map.put("status",StatusConstant.DISABLE);
        Integer discontinued=dishMapper.getcount(map);
        DishOverViewVO  dishOverViewVO= DishOverViewVO.builder().discontinued(discontinued).//已停售菜品数量
                sold(sold).build();//已起售菜品数量
        return dishOverViewVO;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        //封装查询map
        HashMap<Object, Object> map = new HashMap<>();
        //已起售套餐数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold=setmealMapper.getcount(map);
        //已停售套餐数量
        map.put("status",StatusConstant.DISABLE);
        Integer discontinued=setmealMapper.getcount(map);
        SetmealOverViewVO  setmealOverViewVO= SetmealOverViewVO.builder().discontinued(discontinued).//已停售套餐数量
                sold(sold).build();//已起售套餐数量
        return setmealOverViewVO;
    }
}
